# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=binutils
_base_ver=2.25
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
# replaces="${MINGW_PACKAGE_PREFIX}-${_realname}=${_base_ver}"
pkgver=2.25.r82131.aae60b2
pkgrel=1
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgdesc="A set of programs to assemble and manipulate binary and object files"
arch=('any')
url="https://www.gnu.org/software/binutils/"
license=('GPL')
#groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib")
#checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib" "git")
options=('staticlibs' '!distcc' '!ccache' 'debug' '!strip')
#install=binutils.install
source=("${_realname}"::"git+https://sourceware.org/git/binutils-gdb.git#branch=${_realname}-${_base_ver//./_}-branch"
        0001-MinGW-w64-Two-fixes-for-unusual-files.patch
        0002-MinGW-w64-Fix-libiberty-makefile.patch
        0003-MinGW-w64-Fix-libibery-configure.patch
        0004-MinGW-w64-Use-gnu-printf.patch
        0005-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch)
sha256sums=('SKIP'
            '6f3b8bdaa41b3d2a7ab98fb819872fa988ebab580daf21522c32012530465e95'
            '5b65eb12154216ccef02c93417468fedd2dd6b9029bb392e5616793136c1abb4'
            '80a0934e863a211b4d76dcf24e8d61841b06d1f8094f3d657d46e337cae6c7f4'
            '824d4cd28976127e0164216c0770bcad0aea29c95805e1e883f93a2cfc4ace3c'
            'bd38317b28b894d5c0e0ba043fc2445af415f6e96bb9ac93c348748d21a43625')

pkgver() {
  cd "$srcdir/$_realname"
  printf "%s.r%s.%s" "${_base_ver}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/${_realname}
  git am "${srcdir}"/0001-MinGW-w64-Two-fixes-for-unusual-files.patch
  git am "${srcdir}"/0002-MinGW-w64-Fix-libiberty-makefile.patch
  git am "${srcdir}"/0003-MinGW-w64-Fix-libibery-configure.patch
  git am "${srcdir}"/0004-MinGW-w64-Use-gnu-printf.patch
  git am "${srcdir}"/0005-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
  rm -rf gdb readline libdecnumber sim
}

build() {
  if [ "${CARCH}" = "x86_64" ]; then
    local _conf='--enable-64-bit-bfd'
  else
    local _conf=''
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
  fi

  cd $srcdir
  rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ../${_realname}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-werror \
    --enable-lto \
    --with-sysroot=${MINGW_PREFIX} \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    $_conf \
    --enable-nls \
    --disable-rpath \
    --disable-multilib \
    --enable-install-libiberty \
    --enable-plugins \
    --enable-gold \
    --disable-shared

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}

  # unset LDFLAGS as testsuite makes assumptions about which ones are active
  # do not abort on errors - manually check log files
  make LDFLAGS="" -k check || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{lib,include}/${_realname}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include
  cp ${pkgdir}${MINGW_PREFIX}/include/ansidecl.h ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/
  mv ${pkgdir}${MINGW_PREFIX}/include/*.h ${pkgdir}${MINGW_PREFIX}/include/${_realname}
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.a ${pkgdir}${MINGW_PREFIX}/lib/${_realname}

  # Remove unwanted files
  find ${pkgdir}${MINGW_PREFIX}/share/locale -type f -iname "bfd.*" -o -iname "opcodes.*" | xargs -rtl1 rm
  # rm -rf ${pkgdir}${MINGW_PREFIX}/share

  # Add some useful headers
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/libiberty.h ${pkgdir}${MINGW_PREFIX}/include
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/demangle.h ${pkgdir}${MINGW_PREFIX}/include

  find  ${pkgdir}${MINGW_PREFIX}/share -type f -iname "opcodes.mo" -o -iname "bfd.mo" | xargs -rtl1 rm
}
